from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from rest_framework import status

from .models import Danmaku
from .serializer import DanmakuSerializer


# Create your views here.

class DanmakuViewSet(ModelViewSet):
    serializer_class = DanmakuSerializer

    def get_queryset(self):
        queryset = Danmaku.objects.all()
        params = self.request.query_params
        if params:
            filters = {}
            for key, value in params.items():
                filters[key] = value
            queryset = queryset.filter(**filters)
        return queryset

    def get_danmaku(self, request, *args, **kwargs):
        video = kwargs.get('video')
        danmakus = Danmaku.objects.filter(video=video)
        results = []
        for danmaku in danmakus:
            # danmaku_ser_item = [str(danmaku.time), danmaku.type, danmaku.color, str(danmaku.author), str(danmaku.text)]
            danmaku_ser_item = DanmakuSerializer(danmaku).data
            results.append(danmaku_ser_item)
        return Response(results,status=status.HTTP_200_OK)